<?php

namespace App\Admin\Controllers\Vote\Player;

use App\Admin\Actions\GoBackButton;
use App\Admin\Renders\CommonGrid;
use App\Models\Player;
use App\Models\Support;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;

class SupportController extends Controller
{
    public function index(Content $content, Player $player): Content
    {
        return $content->header('支持列表')
            ->description($player->name)
            ->body($this->grid($player));
    }

    public function grid(Player $player): Grid
    {
        $model = Support::ofAdmin()
            ->where('player_id', $player->getKey())
            ->with(['user', 'gift']);

        return Grid::make($model, function (Grid $grid) {
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->tools([new GoBackButton()]);

            CommonGrid::support($grid);
        });
    }

    public function destroy(Player $player, int|string $id): JsonResponse
    {
        return $this->form($player)->destroy($id);
    }

    public function form(Player $player): Form
    {
        return Form::make(Support::ofAdmin(), function (Form $form) use ($player) {
            $form->hidden('player_id')
                ->value($player->getKey());
        });
    }
}
